package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.SetMealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetMealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetMealServiceImpl implements SetMealService {
    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetMealDishMapper setMealDishMapper;


    // TODO：setmealDishes数据封装
    @Override
    public SetmealVO getById(Long id) {
        // 封装普通属性
        SetmealVO setmealVO = setmealMapper.getById(id);
        // 封装来自其他表的属性
        List<SetmealDish> setmealDishes = setMealDishMapper.getById(id);
        setmealVO.setSetmealDishes(setmealDishes);

        return setmealVO;
    }

    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        int page = setmealPageQueryDTO.getPage();
        int pageSize = setmealPageQueryDTO.getPageSize();
        PageHelper.startPage(page,pageSize);

        List<SetmealVO> setmealVOList = setmealMapper.pageQuery(setmealPageQueryDTO);
        Page<SetmealVO> p = (Page<SetmealVO>) setmealVOList;
        PageResult pageResult = new PageResult(p.getTotal(), p.getResult());
        return pageResult;

    }

    @Override
    public void status(String status, Long id) {
        setmealMapper.status(status,id);
    }

    @Override
    @Transactional
    // 新增套餐时还要把包含的菜品新增进数据库
    public void insert(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        // 插入完成后拿到主键值，即setmeal_id
        setmealMapper.insert(setmeal);
        Long setmealId = setmeal.getId();

        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (setmealDishes != null && setmealDishes.size() > 0){
            setmealDishes.stream().forEach(setmealDish -> {
                setmealDish.setSetmealId(setmealId);
            });
        }
        setMealDishMapper.insert(setmealDishes);
    }

    @Override
    public void delete(Long[] ids) {
        // 起售的套餐不能删除
        for (int i = 0; i < ids.length; i++) {
            SetmealVO setmealVO = getById(ids[i]);
            if (setmealVO.getStatus() == 1){
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }
        // 删除时还要删除setmeal_dish下相关联的菜品
        setmealMapper.delete(ids);
        setMealDishMapper.delete(ids);
    }

    @Override
    public void update(SetmealDTO setmealDTO) {
        // 更新普通字段
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.update(setmeal);

        // 更新相关联的setmeal_dish中的信息
        // 先全部删除
        Long id = setmealDTO.getId();
        Long[] ids = {id};
        setMealDishMapper.delete(ids);

        // 再插入新的
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (setmealDishes != null && setmealDishes.size() >0){
            setmealDishes.stream().forEach(setmealDish -> {
                setmealDish.setSetmealId(id);
            });
            setMealDishMapper.insert(setmealDishes);
        }
    }

    /**
     * 根据分类id查询套餐
     * @param setmeal
     * @return
     */
    @Override
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> setmeals = setmealMapper.list(setmeal);
        return setmeals;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    @Override
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
